package com.itxiaowu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itxiaowu.constant.MessageConstant;
import com.itxiaowu.dao.MemberDao;
import com.itxiaowu.dao.OrderDao;
import com.itxiaowu.dao.OrderSettingDao;
import com.itxiaowu.entity.Result;
import com.itxiaowu.pojo.Member;
import com.itxiaowu.pojo.Order;
import com.itxiaowu.pojo.OrderSetting;
import com.itxiaowu.service.OrderService;
import com.itxiaowu.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;
    /**
     * 提交预约数据
     *
     * @param map
     * @return
     */
    @Override
    public Result submitOrder(Map map) throws Exception {

        String orderDate = String.valueOf(map.get("orderDate"));

        Date date = DateUtils.parseString2Date(orderDate);
        // 查询的预约设置
        OrderSetting orderSetting = orderSettingDao.findByDate(date);

        // 1: 判断当前日期是否可以预约,
        if(orderSetting == null){
            return new Result(false,MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        // 2: 判断是否已经预约满了
        if(orderSetting.getNumber() <= orderSetting.getReservations()){
            return new Result(false,MessageConstant.ORDER_FULL);
        }


        // 3: 判断用户是否已经预约
        //  根据用户电话来查看是否为会员
        String telephone = String.valueOf(map.get("telephone"));
        String setmealId = String.valueOf(map.get("setmealId"));

        Member member = memberDao.findByTelephone(telephone);
        // 4: 如果用户是会员, 则直接完成注册, 不是,则注册为会员

        if(member != null){
            // 查询order 的数据判断

            Order order = new Order(member.getId(),date,Integer.valueOf(setmealId));
            List<Order> list = orderDao.findByOrder(order);

            if(list != null && list.size() > 0){
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }else{
            // 不是会员进行注册
            member = new Member();
            member.setName(String.valueOf(map.get("name")));
            member.setIdCard(String.valueOf(map.get("idCard")));
            member.setPhoneNumber(String.valueOf(telephone));
            //注册事件
            member.setRegTime(new Date());

            member.setSex(String.valueOf(map.get("sex")));
            //进行添加
            memberDao.insertMember(member);
        }
        // 5: 修改预约人数
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservations(orderSetting);

        // 添加预约信息
        Order order = new Order(member.getId(),date,(String) map.get("orderType"),(String)map.get("orderStatus"),Integer.valueOf(setmealId));

        orderDao.insert(order);

        /**
         *  返回预约信息
         */
        System.out.println("主键值是：" + order.getId());
        return new Result(true,MessageConstant.ORDER_SUCCESS, order.getId());

    }

    /**
     * 根据预约id查询数据
     *
     * @param id
     * @return
     */
    @Override
    public Map findById(Integer id) {
        return orderDao.findById(id);
    }

    /**
     * 根据套餐id查询预约信息
     *
     * @param id
     * @return
     */
    @Override
    public Integer findCountBySetmealId(Integer id) {
        return orderDao.findCountBySetmealId(id);
    }


}
